using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

namespace Main
{
    public class Comment
    {
        public Comment()
        {
        }

        private int _ID = 0;
        public int ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }

        private string _Title = "";
        public string Title
        {
            get
            {
                return _Title;
            }
            set
            {
                _Title = value;
            }
        }

        private string _Text = "";
        public string Text
        {
            get
            {
                return _Text;
            }
            set
            {
                _Text = value;
            }
        }

        private Guid _UserID;
        public Guid UserID
        {
            get
            {
                return _UserID;
            }
            set
            {
                _UserID = value;
            }
        }
        private int _PollID;
        public int PollID
        {
            get
            {
                return _PollID;
            }
            set
            {
                _PollID = value;
            }
        }
        private DateTime _CreatedOn;
        public DateTime CreatedOn
        {
            get
            {
                return _CreatedOn;
            }
            set
            {
                _CreatedOn = value;
            }
        }
        public void Commit()
        {
            try
            {
                DataAccess.CreateComment((Guid)UserID, PollID, Text, Title);
            }
            catch (Exception e)
            {
                String test = e.Message;
            }
        }
        public void FillByID(int p_CommentID)
        {
            DataTable dt = DataAccess.GetCommentByID(p_CommentID);

            if (dt.Rows.Count > 0)
            {
                ID = (int)dt.Rows[0]["CommentID"];
                Text = (string)dt.Rows[0]["Comment"];
                PollID = (int)dt.Rows[0]["PollID"];
                UserID = (Guid)dt.Rows[0]["UserID"];
                CreatedOn = (DateTime)dt.Rows[0]["DateCreated"];
            }
        }

    }

    public class CommentCollection : CollectionBase
    {
        public void Add(Comment c_Comment)
        {
            InnerList.Add(c_Comment);
        }
        public void Remove(int c_Index)
        {
            InnerList.Remove(c_Index);
        }

        public Comment this[int index]
        {
            get
            { return (Comment)InnerList[index]; }
        }

        public void GetCommentsForPoll(int poll)
        {
            DataTable dt = DataAccess.GetCommentsForPoll(poll);
            int currentComment = 0;
            foreach (DataRow row in dt.Rows)
            {
                Comment newComment = new Comment();
                newComment.PollID = (int)row[dt.Columns.IndexOf("PollID")];
                newComment.Title = (string)row[dt.Columns.IndexOf("Title")];
                newComment.Text = (string)row[dt.Columns.IndexOf("Comment")];
                newComment.UserID = (Guid)row[dt.Columns.IndexOf("UserID")];
                newComment.CreatedOn = (DateTime)row[dt.Columns.IndexOf("DateCreated")];
                InnerList.Add(newComment);
            }
        }
    }
}
